package com.xdk.templatescala.controller;

import cn.hutool.core.util.ObjectUtil;
import com.dtflys.forest.annotation.Delete;
import com.xdk.templatescala.common.response.Page;
import com.xdk.templatescala.common.response.ResultCode;
import com.xdk.templatescala.model.ScaleEntry;
import com.xdk.templatescala.service.ScaleService;
import com.xdk.templatescala.common.response.Result;
import com.xdk.templatescala.common.response.ResultBuild;
import com.xdk.templatescala.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @menu 模板
 * @author zhanghao
 **/
@RestController
@RequestMapping("/scale")
public class ScaleController {

    @Autowired
    private ScaleService scaleService;
    /**
     * 新增模板
     *
     * @return
     */
    @PostMapping(value = "/addScale")
    public Result<ScaleEntry> addScale(@RequestBody ScaleEntry scaleEntry) {
        return ResultBuild.success(scaleService.addScaleEntry(scaleEntry));
    }

    /**
     * 更新模板
     *
     * @return
     */
    @PostMapping(value = "/updateScale")
    public Result<Long> updateScale(@RequestBody ScaleEntry scaleEntry) {
        if(ObjectUtil.isEmpty(scaleEntry.getScaleId())){
           return ResultBuild.fail(ResultCode.PARAM_NOT_NULL,"ID不能为空");
        }
        return ResultBuild.success(scaleService.updateScale(scaleEntry));
    }


    /**
     * 获取所有模板
     *
     * @return
     */
    @GetMapping(value = "/getAllScale")
    public Result<List<ScaleEntry>> getAllScale(@RequestParam(value = "pageNum", required = false) Integer pageNum,
                                                @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        if (StringUtils.isNull(pageNum)){
            pageNum = 1;
        }
        if (StringUtils.isNull(pageSize)){
            pageSize = 10;
        }
        Page<ScaleEntry> page = scaleService.findScaleEntryByParam(pageNum, pageSize);
        return ResultBuild.success(page.getList(), page.getTotalSize());
    }

    /**
     * 根据scaleId获取模板
     *
     * @return
     */
    @GetMapping(value = "/getScale/{scaleId}")
    public Result<ScaleEntry> getScale(@PathVariable("scaleId") String scaleId) {
        return ResultBuild.success(scaleService.findScaleEntry(scaleId));
    }

    /**
     * 根据scaleIds数组删除模板
     *
     * @return
     */
    @DeleteMapping(value = "/delScale/{scaleIds}")
    public Result<Long> delScale(@PathVariable("scaleIds") String scaleIds) {
        return ResultBuild.success(scaleService.delScales(scaleIds));
    }
}
